package com.bilibili.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommenResult;
import com.bilibili.service.DanmakuService;
import com.bilibili.service.impl.DanmakuServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/updateDanmakuStatus.do")
public class UpdateDanmakuStatusServlet extends HttpServlet {
    private DanmakuService danmakuService = new DanmakuServiceImpl();
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        
        try {
            // 获取请求参数
            String danmakuIdStr = request.getParameter("danmakuId");
            String statusStr = request.getParameter("status");
            
            if (danmakuIdStr == null || statusStr == null) {
                CommenResult result = new CommenResult(400, "参数缺失", null);
                out.write(objectMapper.writeValueAsString(result));
                return;
            }
            
            int danmakuId = Integer.parseInt(danmakuIdStr);
            int status = Integer.parseInt(statusStr);
            
            // 调用服务层更新状态
            boolean success = danmakuService.updateDanmakuStatus(danmakuId, status);
            
            // 返回结果
            CommenResult result;
            if (success) {
                result = new CommenResult(200, "状态更新成功", null);
            } else {
                result = new CommenResult(500, "状态更新失败", null);
            }
            
            out.write(objectMapper.writeValueAsString(result));
        } catch (Exception e) {
            e.printStackTrace();
            CommenResult result = new CommenResult(500, "服务器错误", null);
            out.write(objectMapper.writeValueAsString(result));
        } finally {
            out.close();
        }
    }
}